﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Controller;
using EntityObjects;

namespace PresentationLayer.View.StoreClerk
{
    public partial class AssignDisbursementItem : System.Web.UI.Page
    {
        ConsolidateRetrivalControl control;
        static string itemId="";
        int neededqty;
        int totRetrieveQty;
        int leftQty;

        protected void Page_Load(object sender, EventArgs e)
        {
            control = new ConsolidateRetrivalControl();
            lbError.Visible = false;

            //get display data
            string qs = Request.QueryString["ItemId"];
            string[] temp = qs.Split('_');
            itemId = temp[0];
            lbNeededQty.Text = temp[1];
            neededqty = Convert.ToInt32(temp[1]);
            hdnItemId.Value = itemId;
            Catalogue rst = control.getCatalogueByItem(itemId);
            lbItem.Text = rst.Description;
            Category cg = control.getCategoryById(rst.CategoryId);
            lbCategory.Text = cg.CategoryName;
           
            if (!IsPostBack)
            {
                gvAssignList.DataSource = control.getDataForAssignment(itemId);
                gvAssignList.DataBind();
            }
            //int totRetrieveQty=0;
            //if(Session["totretrievedqty"] !=null)
            //    totRetrieveQty= (int)Session["totretrievedqty"];
            //if (totRetrieveQty != 0)
            //{
            //    txtRetrieveQty.Visible = true;
            //    btnUpdateQtyInventory.Visible = true;
            //    lbAqty.Text = totRetrieveQty.ToString();
            //    Panel1.Visible = true;
            //}
            //else
            //{
            //    Panel1.Visible = false;
            //}
            
        }

        protected void gvAssignList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Assign")
            {
                                
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvAssignList.Rows[index];

                HiddenField hdn = (HiddenField)row.FindControl("hdnDeptId");
                string deptID = hdn.Value.ToString();
                TextBox txt = (TextBox)row.FindControl("txtAssignQty");
                int assignQty=0;
                if (txtRetrieveQty.Text == "")
                {
                    lbError.Text = "[Enter Retrieved Qty]";
                    lbError.Visible = true;
                }
                else
                {
                totRetrieveQty = Convert.ToInt32(txtRetrieveQty.Text);

                //check validation
                int number;
                string value = txt.Text;
                bool result = Int32.TryParse(value, out number);
                if (!result)
                {
                    lbError.Text = "Assigned Quantity only accept integer value";
                    lbError.Visible = true;
                    ((TextBox)row.FindControl("txtAssignQty")).Text = null;
                    ((TextBox)row.FindControl("txtAssignQty")).Focus();
                }
                else
                {
                    assignQty = Convert.ToInt32(txt.Text); 
                    if (assignQty <= 0 || assignQty > totRetrieveQty)
                    {
                        lbError.Text = "Assigned Quantity should be more than 0 and less than equal retrieved quantity";
                        lbError.Visible = true;
                    }
                    else
                    {
                        leftQty = (int)ViewState["leftQty"];
                        //if (assignQty > leftQty)
                        //{
                        //    lbError.Text = "[Assigned Quantity is not enough]";
                        //    lbError.Visible = true;
                        //}
                        //else
                        //{
                        leftQty = leftQty - number;
                        ViewState["leftQty"] = leftQty;
                        lbLQty.Text = leftQty.ToString();//showing left qty label

                        HiddenField hdntotqty = (HiddenField)row.FindControl("hdnTotReqQty");
                        int totqty = Convert.ToInt32(hdntotqty.Value);

                        //store in session
                        List<DisbursementDetails> ddisList = (List<DisbursementDetails>)Session["DisbursementDetails"];
                        if (ddisList != null && ddisList.Count == 0)
                        {
                            DisbursementDetails d = new DisbursementDetails();
                            d.ItemId = itemId;
                            d.RequestedQty = totqty;
                            d.ReceivedQty = assignQty;
                            d.DeptId = deptID;
                            ddisList.Add(d);
                            Session["DisbursementDetails"] = ddisList;
                        }
                        else
                        {
                            ddisList = new List<DisbursementDetails>();
                        }

                        //4.2.2012
                        bool flag = false;
                        if(ddisList!=null)
                        for (int i = 0; i < ddisList.Count; i++)
                        {//to check already exit in session
                            if (ddisList[i].DeptId.Equals(deptID))
                            {
                                ddisList[i].ReceivedQty = assignQty;
                                flag = true;
                            }
                        }
                        if (!flag)
                        {
                            DisbursementDetails d = new DisbursementDetails();
                            d.ItemId = itemId;
                            d.RequestedQty = totqty;
                            d.ReceivedQty = assignQty;
                            d.DeptId = deptID;
                            ddisList.Add(d);
                            Session["DisbursementDetails"] = ddisList;
                        }

                        //store in session
                        List<Disbursement> disList;
                        disList = (List<Disbursement>)Session["Disbursement"];
                        if (disList == null)
                        {
                            disList = new List<Disbursement>();
                        }
                        Department dept = control.getDepartment(deptID);

                        Disbursement dis = new Disbursement();
                        dis.DeptId = deptID;
                        dis.DisburseDate = DateTime.Today;
                        dis.Status = "Assigned";
                        dis.RepresentativeId = dept.RepresentativeId;
                        dis.CollectionId = dept.CollectionId;
                        disList.Add(dis);
                        Session["Disbursement"] = disList;
                        //}
                        }
                    }
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            List<DisbursementDetails> ddisList = (List<DisbursementDetails>)Session["DisbursementDetails"];
            if (ddisList == null || ddisList.Count == 0)
            {
                lbError.Text = "Assign Item first and then submit again";
                lbError.Visible = true;
            }
            else
            {
                int retrieveQty = Convert.ToInt32(txtRetrieveQty.Text);
                int totqty = 0;
                foreach (DisbursementDetails dd in ddisList)
                {
                    totqty += dd.ReceivedQty;
                }
                if (totqty > retrieveQty)
                {
                    lbError.Text = "Total assigned Quantity is more than Retrieved Quantity";
                    lbError.Visible = true;
                }
                else
                {
                    string itemid = hdnItemId.Value;
                    Boolean flag = control.updateRetrievedQtyinCatalogue(itemid, retrieveQty);
                    //saved quantity

                    List<Disbursement> dList = (List<Disbursement>)Session["Disbursement"];

                    GridView2.DataSource = dList;
                    GridView2.DataBind();
                    control.SaveDisbursement(dList);

                    List<DisbursementDetails> disdetail = (List<DisbursementDetails>)Session["DisbursementDetails"];
                    GridView3.DataSource = disdetail;
                    GridView3.DataBind();
                    if (disdetail.Count != 0)
                        control.SaveDisbursementDetail(disdetail);

                    if (flag)
                    {
                        Session["Disbursement"] = new List<Disbursement>();
                        Session["DisbursementDetails"] = new List<DisbursementDetails>();
                        Session["totretrievedqty"] = 0;
                        Session["leftQty"] = 0;
                        Response.Redirect("ViewRetrievalForm.aspx");
                    }
                }
            }
        }


        protected void txtRetrieveQty_TextChanged(object sender, EventArgs e)
        {
            int number;
            string value = txtRetrieveQty.Text;
            bool result = Int32.TryParse(value, out number);
            if (!result)
            {
                Label5.Text = "Retrieved Quantity only accept integer value";
                Label5.Visible = true;
                txtRetrieveQty.Text = null;
                txtRetrieveQty.Focus();
            }
            else
            {
                if (number <= 0)
                {
                    Label5.Text = "Retrieved Quantity should be positive interget value";
                    Label5.Visible = true;
                    txtRetrieveQty.Text = null;
                    txtRetrieveQty.Focus();
                }
                else if (number > neededqty)
                {
                    Label5.Text = "Retrieved Quantity should not more than Needed Quantity";
                    Label5.Visible = true;
                    txtRetrieveQty.Text = null;
                    txtRetrieveQty.Focus();
                }
                else
                {
                    totRetrieveQty = number;
                    leftQty = number;
                    ViewState["leftQty"] = leftQty;
                    lbLQty.Text = totRetrieveQty.ToString();
                    lbError.Visible = false;
                    Label5.Visible = false;
                }
                
            }
        }
    }
}